﻿using System;
using UnityEngine;

namespace HKTools
{
    /// <summary>
    /// 摄像机多目标切换管理工具，RotTargetItem 控制脚本
    /// </summary>
    public class HK_RotTarget : HK_BaseTarget
    {
        /// <summary>
        /// 获取目标位置，基于当前对象的位置和朝向以及目标距离
        /// </summary>
        public Vector3 TargetPos
        {
            get { return transform.position + transform.forward * distance; }
        }

        /// <summary>
        /// 获取目标方向，从当前对象指向目标位置
        /// </summary>
        public Vector3 TargetDir
        {
            get { return transform.position - TargetPos; }
        }

        [Header("默认距离（Green）：")]
        public float distance = 2;
        [Header("最近距离（Blue）：")]
        public float minDis;
        [Header("最远距离（Red）：")]
        public float maxDis;

        [Header("边界高度："), SerializeField, Range(10, 20)]
        float height = 15;
        [Header("边界长度："), SerializeField, Range(10, 20)]
        float length = 15;

        [Header("左边界角度："), Range(0, 180)]
        public float leftRotLimit;
        [Header("右边界角度："), Range(0, 180)]
        public float rightRotLimit;

        public override void Awake()
        {
            base.Awake();
            TargetKind = HK_TargetKind.RotTarget;
        }

        // 绿色为默认目标位置，红色为最远位置，蓝色为最近位置
        void OnDrawGizmosSelected()
        {
            var radius = 1f;

            if (minDis > distance)
                minDis = distance;

            if (leftRotLimit == 180)
                leftRotLimit = -179;

            if (rightRotLimit == 180)
                rightRotLimit = 179;

            Gizmos.color = Color.blue;
            Gizmos.DrawRay(transform.position, transform.forward * minDis);
            Gizmos.DrawSphere(transform.position + transform.forward * minDis, radius);

            Gizmos.color = Color.green;
            Gizmos.DrawRay(transform.position + transform.forward * minDis, transform.forward * (distance - minDis));
            Gizmos.DrawSphere(transform.position + transform.forward * distance, radius);

            if (maxDis < distance)
                maxDis = distance;

            Gizmos.color = Color.red;
            Gizmos.DrawRay(transform.position + transform.forward * distance, transform.forward * (maxDis - distance));
            Gizmos.DrawSphere(transform.position + transform.forward * maxDis, radius);

            //画线范围
            if (leftRotLimit != 0 || rightRotLimit != 0)
            {
                DrawWireSemicircle(transform.position, Vector3.ProjectOnPlane(transform.forward, Vector3.up), -leftRotLimit, rightRotLimit, length, height, Vector3.up);
            }
        }

        //绘制左右角度
        void DrawWireSemicircle(Vector3 origin, Vector3 direction, float leftAngle, float rightAngle, float length, float height, Vector3 axis)
        {
            Gizmos.color = Color.red;
            //左侧
            Vector3 leftdir = Quaternion.AngleAxis(-Mathf.Abs(leftAngle), axis) * direction;
            //右侧
            Vector3 rightdir = Quaternion.AngleAxis(rightAngle, axis) * direction;

            Vector3 curPoint = origin + leftdir.normalized * length;

            // 绘制左面片
            Gizmos.DrawLine(origin, origin + Vector3.up * height);
            Gizmos.DrawLine(origin + Vector3.up * height, curPoint + Vector3.up * height);
            Gizmos.DrawLine(origin, curPoint);
            Gizmos.DrawLine(curPoint + Vector3.up * height, curPoint);

            curPoint = origin + rightdir.normalized * length;

            // 绘制右面片
            Gizmos.DrawLine(origin, origin + Vector3.up * height);
            Gizmos.DrawLine(origin + Vector3.up * height, curPoint + Vector3.up * height);
            Gizmos.DrawLine(origin, curPoint);
            Gizmos.DrawLine(curPoint + Vector3.up * height, curPoint);
        }
    }
}